package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.CategoryBiz;
import entity.Category;
import util.BaseServlet;

/**
 * Servlet implementation class AdminCategory
 */
public class AdminCategoryServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
	CategoryBiz cate =new CategoryBiz();
   
	public String findAll(HttpServletRequest req, HttpServletResponse res)
	{
		req.setAttribute("parents",cate.findparentsAndChildren());
		return "f:/adminjsps/admin/category/list.jsp";
	}
	
	public String addParent(HttpServletRequest req, HttpServletResponse res)
	{
		String cname = req.getParameter("cname");
		String desc =req.getParameter("desc");
		Category cate2 =new Category(cname,desc);
		cate.insertcate(cate2);
		
		return findAll(req, res);	
	}
	
	public String addChildPre(HttpServletRequest req, HttpServletResponse res)
	{
		String ppid = req.getParameter("pid");
		//Integer pid =Integer.parseInt(ppid);
		req.setAttribute("pid", ppid);
		req.setAttribute("parents",cate.findparents());
		return "f:/adminjsps/admin/category/add2.jsp";
	}
	
	public String addChild(HttpServletRequest req, HttpServletResponse res)
	{
		String cname = req.getParameter("cname");
		String des =req.getParameter("desc");
		String ppid = req.getParameter("pid");
		Integer pid =Integer.parseInt(ppid);
		cate.inserttwocate(new Category(cname,new Category(pid),des));
		return findAll(req, res);
	}
	
	
	public String editParentPre(HttpServletRequest req, HttpServletResponse res)
	{
		String ccid = req.getParameter("cid");
		Integer cid =Integer.parseInt(ccid);
		req.setAttribute("parent",cate.findCategoryById(cid));
		return "f:/adminjsps/admin/category/edit.jsp";
	}
	
	public String editParent(HttpServletRequest req, HttpServletResponse res)
	{
		String ccid = req.getParameter("cid");
		Integer cid =Integer.parseInt(ccid);
		String cname =req.getParameter("cname");
		String des =req.getParameter("desc");
		cate.updatefathercate(new Category(cid,cname,des));
		return findAll(req, res);
	}
	
	public String deleteParent(HttpServletRequest req, HttpServletResponse res)
	{
		String ccid = req.getParameter("cid");
		Integer cid =Integer.parseInt(ccid);
		cate.deletechildrencate(cid);
		cate.deletefathercatebyid(cid);
		return findAll(req, res);
	}
	public String editChildPre(HttpServletRequest req, HttpServletResponse res)
	{
		String ccid = req.getParameter("cid");
		Integer cid =Integer.parseInt(ccid);
		req.setAttribute("child", cate.findchildcatebyid(cid));
		req.setAttribute("parents", cate.findparents());
		
	    return "f:/adminjsps/admin/category/edit2.jsp";
	
	
	}
	
	public String editChild(HttpServletRequest req, HttpServletResponse res)
	{
		String ccid = req.getParameter("cid");
		Integer cid =Integer.parseInt(ccid);
		String ppid = req.getParameter("pid");
		Integer pid =Integer.parseInt(ppid);
		String cname =req.getParameter("cname");
		String des =req.getParameter("desc");
		cate.updatechildcate(new Category(cid, cname, new Category(pid), des));
		return findAll(req, res);
	}
	
	public String deleteChild(HttpServletRequest req, HttpServletResponse res)
	{      
		String ccid = req.getParameter("cid");
	    Integer cid =Integer.parseInt(ccid);
		cate.deletechildcateById(cid);
		return findAll(req, res);
	}
}
